define(['jquery', 'eventbus', 'dataservice'], function ($, eventbus, ds) {
	return function () {

		// Navigate to user details and notify the other components.
		let navigateToUserDetails = function (data) {
			localStorage.setItem("userName", data.name);
			localStorage.setItem("userEmail", data.email);
			eventbus.publish("changeComponentEvent", "user-details");
		}

		// Check if we have an authentication token
		// and change the active component if we have a valid one.
		if (localStorage.getItem("token") !== null) {
			ds.getAccountDetails(function (data) {
				if (data.name !== null || data.name !== undefined) {
					navigateToUserDetails(data);
				}
			}, true);
		}

		// Click callback for the registration link.
		let onRegisterLinkClick = function () {
			eventbus.publish("changeComponentEvent", "registration");
		}

		// Click callback for the login button.
		let onLoginClick = function () {
			// TODO: do we really need jQuery here? why not do it with knockout.
			let loginData = {
				email: $("#email").val(),
				password: $("#password").val()
			}

			// Try to login.
			ds.postLoginAttempt(function (data) {
				localStorage.setItem("token", data.token);
				navigateToUserDetails(data);
			}, loginData);
		}

		// Expose the properties needed by the UI.
		return {
			title: 'Login',
			onRegisterLinkClick,
			onLoginClick
		}
	}
});